#include "UnorderedSet.hpp"
#include "UnorderedMap.hpp"
#include <iostream>
using namespace std;

void test1(){
  zty::unordered_set<int> us;
  us.insert(1);
  us.insert(6);
  cout << "_size: " << us.size() << endl;
  us.insert(5);
  us.insert(8);
  cout << "_size: " << us.size() << endl;

  auto it = us.begin();
  while(it != us.end())
  {
    cout << *it << " ";
    ++it;
  }
  cout << endl;

  us.erase(1);
  cout << "_size: " << us.size() << endl;
  us.erase(8);
  cout << "_size: " << us.size() << endl;
  
  it = us.begin();
  while(it != us.end())
  {
    cout << *it << " ";
    ++it;
  }
  cout << endl;

  //--it;
  //while(it != us.begin())
  //{
  //  cout << *it  << " ";
  //  --it;
  //}
  //cout << endl;

  //for(int e : us)
  //{
  //  cout << e << " ";
  //}
  //cout << endl;
}

void Dictionary(){
  zty::unordered_map<string, string> dict;
  //map存储的元素是键值对pair，有以下几种pair的构造插入方法：
  //1.匿名对象构造法
  dict.insert(pair<string, string>("string", "字符串"));
  //2.typedef类型重命名，缩短类型名
  typedef pair<string, string> DictKV;
  dict.insert(DictKV("peach", "桃子")); 
  //3.通过make_pair函数模版构造键值对（推荐）   
  dict.insert(make_pair("dictionary", "字典"));    
  dict.insert(make_pair("temporary", "短暂的"));
  dict.insert(make_pair("declaration", "声明"));    
    
  //map的迭代器遍历
  //map<string, string>::iterator it = dict.begin();                   
  auto it = dict.begin(); //map迭代器的类型过长，通过auto自动识别类型    
  while(it != dict.end())                        
  {    
    //cout << (*it).first << ":" << (*it).second << endl; //解引用后通过.访问pair的成员    
    cout << it->first << ":" << it->second << endl;//直接通过->访问pair的成员（推荐）  
    ++it;    
  } 
   
  //范围for:
  //for(pair<const string, string> &e : dict) //map存储的元素是键值对pair，注意key是const类型
  //for(auto &e : dict) //临时变量e应该取引用，避免发生拷贝构造，浪费资源   
  //{    
  //  cout << e.first << ":" << e.second << endl; 
  //}
    
  string input;
  while(cin >> input)                     
  {
    auto ret = dict.find(input); //传入key值，返回对应元素(pair)的迭代器
    if(ret != dict.end()) //找不到返回map::end
    {
      cout << ret->first << ":" << ret->second << endl;
    }
    else{
      cout << "There is no such word!" << endl;
    }
  }
}

void WordCounter(){
  zty::unordered_map<string, int> counter;
  string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉"};
  //方法一：上一章讲过的方法
  //for(string &e : arr)
  //{
  //  auto ret = counter.find(e);
  //  if(ret != counter.end())
  //  {
  //    ++ret->second; 
  //  } 
  //  else{
  //    counter.insert(make_pair(e,1));                                                                                          
  //  }
  //}
    
  //方法二：operator[]（推荐）
  for(string &e : arr)
  {
    //如果e不在counter中，先插入pair(e,int())，再对返回value(次数)++;
    //如果e在counter中，返回value(次数)的引用，次数++;
    ++counter[e];
  }
  for(auto &e : counter)
  {
    cout << e.first << ":" << e.second << endl;
  }
}


int main(){
  test1();
  WordCounter();
  Dictionary();
  return 0;
}
